import redis.asyncio as redis


class RedisManager:
    def __init__(self):
        self.pool = None
        self.client = None

    async def connect(self, host="localhost", port=16379,db=0, **kwargs):
        """建立Redis连接"""
        self.pool = redis.ConnectionPool(
            host=host,
            port=port,
            db=db,
            decode_responses=True,
            **kwargs
        )
        self.client = redis.Redis(connection_pool=self.pool)

        # 测试连接
        await self.client.ping()
        print("Redis连接成功")

    async def disconnect(self):
        """关闭Redis连接"""
        if self.client:
            await self.client.close()
            print("Redis连接已关闭")

    @property
    def redis(self):
        """返回Redis客户端"""
        return self.client